package info.hexin.mvc.handler.process;

import info.hexin.log.Log;
import info.hexin.log.Logs;
import info.hexin.mvc.annotation.HeaderParam;
import info.hexin.mvc.annotation.MultiFile;
import info.hexin.mvc.annotation.Param;
import info.hexin.mvc.annotation.PathParam;
import info.hexin.mvc.methodtree.Arg;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 文件上传验证
 * 
 * @author ydhexin@gmail.com
 * 
 */
public class DefaultRequestProcess extends AbstractReequestProcess {
    private static final Log log = Logs.get();

	public void doMethodParam(HttpServletRequest request, HttpServletResponse response, List<Arg> args,
			List<Object> argList) {
		String requestUrl = request.getRequestURI();
		String tempRequestUrl = requestUrl.substring(1);
		String[] urls = tempRequestUrl.split("/");

		for (Arg arg : args) {
			Class<?> argAnClazz = arg.getAnClass();
			if (argAnClazz == null) {
				Class<?> paramClass = arg.getParamClass();
				if (paramClass == HttpServletRequest.class) {
					argList.add(request);
				} else if (paramClass == HttpSession.class) {
					argList.add(request.getSession());
				} else if (paramClass == HttpServletResponse.class) {
					argList.add(response);
				}
			} else {
				String paramName = arg.getName();
				if (argAnClazz == HeaderParam.class) {
					String value = request.getHeader(paramName);
					argList.add(value);
				} else if (argAnClazz == Param.class) {
					String value = request.getParameter(paramName);
					argList.add(value);
				} else if (argAnClazz == PathParam.class) {
					int i = arg.getPathIndex();
					String pathValue = urls[i];
					argList.add(pathValue);
				} else if (argAnClazz == MultiFile.class) {
//					TmpFile tmpFile = new TmpFile();
//					argList.add(tmpFile);
				}
			}
		}
	}
}
